package buff

import (
	"encoding/json"
	"errors"
	"fmt"
	"kop/modules/configuration/client"
	"kop/modules/configuration/configuration/bufftype"
	"kop/util/log"
	"kop/util/rand"
)

func init() {

	var b = client.Load("ConquerBuff")
	if err := json.Unmarshal(b, &Configs); err != nil {
		panic(err)
	}

	log.Info("Load ConquerBuff.json success.")
}

type Conf struct {
	ID       int32         // 增益ID
	Name     string        // 增益名称
	Group    int32         // 分组ID
	Quality  int32         // 增益品质
	Weight   int32         // 权重：1:10000
	BuffType bufftype.Type // 效果类型
	Value    float32       // 效果值
	ItemID   int32         // 兑换物品ID
	Price    int32         // 兑换价格
}

func (b Conf) GetWeight() int32 {
	return b.Weight
}

func (b Conf) GetID() int32 {
	return b.ID
}

var Configs = map[int32]Conf{}

func GetByBuffID(BuffID int32) (Conf, error) {

	var conf, ok = Configs[BuffID]
	if ok {
		return conf, nil
	}

	return Conf{}, errors.New(fmt.Sprintf("BuffID not found [%d]", BuffID))
}

func Rand3() []int32 {

	var values1 []rand.Value
	var values2 []rand.Value
	var values3 []rand.Value

	for _, conf := range Configs {
		switch conf.Group {
		case 1:
			values1 = append(values1, conf)
		case 2:
			values2 = append(values2, conf)
		case 3:
			values3 = append(values3, conf)
		}
	}

	var conf1 = rand.WithWeight(values1, 1)[0]
	var conf2 = rand.WithWeight(values2, 1)[0]
	var conf3 = rand.WithWeight(values3, 1)[0]

	//var arr = []rand.Value{conf1, conf2, conf3}
	//
	//var res []int32
	//for _, k := range rand.Shuffle(len(arr)) {
	//	res = append(res, arr[k].GetID())
	//}
	return []int32{conf1.GetID(), conf2.GetID(), conf3.GetID()}
}
